<?php
namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Request;
class ErpController extends AdminController
{
    protected $filters = [];

    private function _getFiltersField(){
        return array_keys($this->filters);
    }

    public function getFilters(){
        $filtersField = $this->_getFiltersField();
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    public function initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    public function getAction(){
        $filtersField = $this->_getFiltersField();
        return getAction($filtersField);
    }

    public function paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    public function getDownloadUrl($downloadUrl){
        return $downloadUrl . '?' . http_build_query(array_merge(Request::all(),['_export_' => true]));
    }

    public function callAjax($obj){
        $action = request('action');
        $param = request('param');
        return $param ? call_user_func([$obj,$action],$param) : call_user_func([$obj,$action]);
    }
}
